package cn.nawang.ebeim.server.util;

import org.apache.log4j.PropertyConfigurator;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class SystemUtil {

  private static final String CONF_PATH = "conf";

  public static String getRootPath() {
    return Class.class.getClass().getResource("/").getPath();
  }

  public static String getConfPath() {
    String rootPath = getRootPath();
    return rootPath + getChangePath(rootPath) + CONF_PATH;
  }
  
  public static String getChangePath(String path) {
    if (path.endsWith("classes") || path.endsWith("classes/")) {
      return "../../";
    }
    return "../";
  }

  public static void initLog() {
    String path = getConfPath() + "/log4j.properties";
    PropertyConfigurator.configure(path);
  }

  public static String getLocalHostIp() {
    try {
      InetAddress ia = InetAddress.getLocalHost();
      if(null == ia){
        return null;
      }
      return ia.getHostAddress();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static String getLocalHostName() {
    try {
      InetAddress ia = InetAddress.getLocalHost();
      if(null == ia){
        return null;
      }
      return ia.getHostName();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static void main(String[] args) {
    System.out.println(SystemUtil.getConfPath());
  }

}
